﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

using Zyg.Utility.Helper;

namespace Zyg.SSO.Core.Business
{
    public class App : BusinessBase<App, Guid>
    {
        public App()
        {
            // Accounts = new AccountAppCollection(null, this, RelationType.ManyToMany);
        }

        #region Fields and Properties

        private string name = String.Empty;
        private string url = String.Empty;
        private string ip = String.Empty;
        private int port = 80;
        private string pictureAddress = String.Empty;
        private string loginFile = String.Empty;

        /// <summary>
        /// Get the name of App.
        /// </summary>
        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    MarkChanged("Name");
                    name = value;
                }
            }
        }

        /// <summary>
        /// Get the url of App.
        /// </summary>
        public string Url
        {
            get { return url; }
            set
            {
                if (url != value)
                {
                    MarkChanged("Url");
                    url = value;
                }
            }
        }

        /// <summary>
        /// Get ip of App.
        /// </summary>
        public String IP
        {
            get { return ip; }
            set
            {
                if (ip != value)
                {
                    MarkChanged("IP");
                    ip = value;
                }
            }
        }

        /// <summary>
        /// Get the port of App.
        /// </summary>
        public int Port
        {
            get { return port; }
            set
            {
                if (value < 1)
                    return;

                if (port != value)
                {
                    MarkChanged("Port");
                    port = value;
                }
            }
        }

        /// <summary>
        /// Get picture of App.
        /// </summary>
        public string PictureAddress
        {
            get { return pictureAddress; }
            set
            {
                if (pictureAddress != value)
                {
                    pictureAddress = value;
                    MarkChanged("PictureAddress");
                }
            }
        }

        /// <summary>
        /// Get the login file path of App.
        /// </summary>
        public string LoginFile
        {
            get { return loginFile; }
            set
            {
                if (loginFile != value)
                {
                    MarkChanged("LoginFile");
                    loginFile = value;
                }
            }
        }

        private AccountAppCollection accounts = null;
        /// <summary>
        /// Get the accounts that own the app.
        /// </summary>
        public AccountAppCollection Accounts
        {
            get
            {
                if (accounts == null)
                    accounts = new AccountAppCollection(null, this, RelationType.ManyToMany);
                return accounts;
            }
            set
            {
                accounts = value;
                MarkChanged("Accounts");
            }
        }

        #endregion

        #region Base overrides

        protected override void ValidationRules()
        {
            AddRule("IDNotNull", "应用标识不合法", Id == System.Guid.Empty);
            AddRule("Name", "应用名称长度必须在1到100之间", !Name.IsInLengthBound(1, 100));
            AddRule("PictureAddress", "图片格式不正确,必须为jp(e)g,gif,bmp或者png格式!", !PictureAddress.IsNullOrEmpty() && !PictureAddress.IsImage());
            AddRule("Url", "应用系统地址不合法", !String.IsNullOrEmpty(Url) && !Url.IsUrl());
            AddRule("IP", "IP地址不合法", !IP.IsIP());
        }

        protected override App DataSelect(Guid id)
        {
            return Services.DALService.SelectApp(id);
        }

        protected override void DataUpdate()
        {
            Services.DALService.UpdateApp(this);
        }

        protected override void DataInsert()
        {
            Services.DALService.InsertApp(this);
        }

        protected override void DataDelete()
        {
            Services.DALService.DeleteApp(this);
        }

        public override SaveAction Save()
        {
            if (this.IsDeleted && !this.IsNew)
            {
                this.Accounts.ClearOnSave();
                this.Accounts.Save();
            }

            SaveAction action = base.Save();

            if (accounts != null)
                Accounts.Save();

            return action;
        }

        #endregion

        #region Static Methods

        public static IList<Business.App> GetAllApps()
        {
            return Services.DALService.FillApps();
        }

        #endregion

        #region Methods

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(String.IsNullOrEmpty(Url) ? String.Format("http://{0}", IP) : Url);

            if (this.Port != 80)
            {
                sb.Append(":");
                sb.Append(this.Port);
            }

            if (!LoginFile.IsNullOrEmpty())
            {
                sb.Append(LoginFile);
            }

            return sb.ToString();
        }

        #endregion
    }
}
